package com.samehope.ar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.dto.PointGroupAddParam;
import com.samehope.ar.dto.PointGroupUpdateParam;
import com.samehope.ar.entity.PatrolPoint;
import com.samehope.ar.entity.PatrolPointGroup;
import com.samehope.ar.mapper.PatrolPointGroupMapper;
import com.samehope.ar.service.IPatrolPointGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.service.IPatrolPointService;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.util.TreeUtils;
import com.samehope.ar.vo.FormSelectVO;
import com.samehope.ar.vo.TreeDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 巡检点分组表 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class PatrolPointGroupServiceImpl extends ServiceImpl<PatrolPointGroupMapper, PatrolPointGroup> implements IPatrolPointGroupService {

    @Autowired
    private IPatrolPointService pointService;

    /**
     * 添加点位分组
     * @param param
     */
    @Override
    public Long add(PointGroupAddParam param) {
        PatrolPointGroup pointGroup = this.baseMapper
                .findByProjectAndGroupName(SessionUtil.getProjectId(), param.getName());

        if (pointGroup != null) {
            throw new IllegalArgumentException("分组名称已存在");
        }

        pointGroup = new PatrolPointGroup();
        pointGroup.setCompanyId(SessionUtil.getCompanyId());
        pointGroup.setProjectId(SessionUtil.getProjectId());
        pointGroup.setName(param.getName());
        pointGroup.setCreateBy(SessionUtil.getUserId());
        pointGroup.setCreateDate(LocalDateTime.now());

        this.save(pointGroup);
        return pointGroup.getId();
    }

    /**
     * 更新点位分组信息
     * @param param
     */
    @Override
    public void update(PointGroupUpdateParam param) {
        PatrolPointGroup pointGroup = this.baseMapper
                .findByProjectAndGroupName(SessionUtil.getProjectId(), param.getName());

        if (pointGroup != null && !pointGroup.getId().equals(param.getGroupId())) {
            throw new IllegalArgumentException("分组名称已存在");
        }

        if (pointGroup == null) {
            pointGroup = this.baseMapper.findByProjectAndGroup(SessionUtil.getProjectId(), param.getGroupId());
        }

        if (pointGroup == null) {
            throw new IllegalArgumentException("分组不存在");
        }

        pointGroup.setName(param.getName());
        pointGroup.setSortNo(param.getSortNo());
        pointGroup.setUpdateBy(SessionUtil.getUserId());
        pointGroup.setUpdateDate(LocalDateTime.now());

        this.updateById(pointGroup);
    }

    /**
     * 删除点位分组
     * @param groupId
     */
    @Override
    public void delete(Long groupId) {
        PatrolPointGroup pointGroup = this.baseMapper
                .findByProjectAndGroup(SessionUtil.getProjectId(), groupId);

        if (pointGroup == null) {
            throw new IllegalArgumentException("分组不存在");
        }

        QueryWrapper<PatrolPoint> qw = new QueryWrapper<>();
        qw.eq("project_id", SessionUtil.getProjectId());
        qw.eq("patrol_point_group_id", groupId);
        qw.eq("del_flag", DelFlagEnum.NORMAL.getCode());
        int count = pointService.count(qw);
        if (count > 0) {
            throw new IllegalArgumentException("分组下有巡检点, 不能删除");
        }

        pointGroup.setDelFlag(DelFlagEnum.DELETED.getCode());
        pointGroup.setUpdateBy(SessionUtil.getUserId());
        pointGroup.setUpdateDate(LocalDateTime.now());

        this.updateById(pointGroup);

    }

    /**
     * 查询巡检点分组树结构
     * @param condition
     * @return
     */
    @Override
    public List<TreeDataVO> getGroupTree(String condition) {
        List<TreeDataVO> tree = this.baseMapper.getGroupTreeByProjectAndCondition(SessionUtil.getProjectId(), condition);
        TreeUtils.setLevel(tree, 1);
        TreeUtils.setId(tree);
        return tree;
    }

    /**
     * 巡检点分组下拉框视图对象
     * @return
     */
    @Override
    public List<FormSelectVO> groupsForSelect() {
        return this.baseMapper.getGroupsForSelectByProject(SessionUtil.getProjectId());
    }

    /**
     * 根据点位分组名称查询点位分组信息
     * @param pointGroupName
     * @return
     */
    @Override
    public PatrolPointGroup findGroupByGroupName(String pointGroupName) {
        return this.baseMapper.findByProjectAndGroupName(SessionUtil.getProjectId(), pointGroupName);
    }
}
